// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var AudioManager = require('AudioManager')
var CreatorHelper = require('CreatorHelper')

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var animation = this.node.addComponent(cc.Animation)
        var url = ['animations/newOutBtn', 'animations/newInBtn']

        CreatorHelper.loadAnimClip(url, (clip) => {
            var outAnimationClip = clip[0]
            var inAnimationClip = clip[1]

            animation.addClip(outAnimationClip, 'out')
            animation.addClip(inAnimationClip, 'in')

            if (this.node !== null) {
                this.node.on(cc.Node.EventType.TOUCH_START, function (args) {
                    animation.play('in')
                    this.loadAndPlayAudio()
                    args.stopPropagation()
                }, this)

                this.node.on(cc.Node.EventType.TOUCH_END, function (args) {
                    animation.play('out')
                    args.stopPropagation()
                }, this)

                this.node.on(cc.Node.EventType.TOUCH_CANCEL, function (args) {
                    animation.play('out')
                    args.stopPropagation()
                }, this)
            }
        })
    },

    start () {

    },

    // 加载并播放音频
    loadAndPlayAudio () {
        AudioManager.playSound('click')
    }

    // update (dt) {},
});
